package com.cskaoyan.service.wxgoods;

import com.cskaoyan.bean.po.ohter.*;
import com.cskaoyan.bean.po.promotionpo.MarketAd;
import com.cskaoyan.bean.po.promotionpo.MarketCoupon;
import com.cskaoyan.bean.po.promotionpo.MarketTopic;
import com.cskaoyan.bean.vo.wxgoods.*;
import com.cskaoyan.mapper.MarketBrandMapper;
import com.cskaoyan.mapper.MarketCategoryMapper;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.promotion.MarketAdMapper;
import com.cskaoyan.mapper.promotion.MarketCouponMapper;
import com.cskaoyan.mapper.promotion.MarketTopicMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @Author Ajun de BBC
 * @Date 2022/7/19 16:43
 * @Version 1.5
 */

@Service
public class WxHomeServiceImpl implements WxHomeService{

    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    @Autowired
    MarketCouponMapper marketCouponMapper;
    @Autowired
    MarketCategoryMapper marketCategoryMapper;
    @Autowired
    MarketAdMapper marketAdMapper;
    @Autowired
    MarketBrandMapper marketBrandMapper;
    @Autowired
    MarketTopicMapper marketTopicMapper;

    @Override
    public HomeIndexVo homeIndexSelectAll() {

        //显示新品首发的商品
        MarketGoodsExample marketNewGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketNewGoodsExample.createCriteria();
        criteria.andIsNewEqualTo(true);
        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketNewGoodsExample);
        int size1 = marketGoods.size();
        Random random1 = new Random();
        List<MarketGoods> marketGoodsList = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            marketGoodsList.add(marketGoods.remove(random1.nextInt(size1 - i)));
        }

        //显示优惠券
        List<MarketCoupon> marketCoupons = marketCouponMapper.selectByExample(null);
        int size2 = marketCoupons.size();
        Random random2 = new Random();
        List<MarketCoupon> marketCouponList = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            marketCouponList.add(marketCoupons.remove(random2.nextInt(size2 - i)));
        }

        //显示商品分类
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(null);
        int size3 = marketCategories.size();
        Random random3 = new Random();
        List<MarketCategory> marketCategoryList = new ArrayList<>();
        for (int i = 0; i < 10 ; i++) {
            marketCategoryList.add(marketCategories.remove(random3.nextInt(size3 - i)));
        }

        //显示广告
        List<MarketAd> marketAds = marketAdMapper.selectByExample(null);

        //显示制造商
        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(null);
        int size4 = marketBrands.size();
        Random random4 = new Random();
        List<MarketBrand> marketBrandList = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            marketBrandList.add(marketBrands.remove(random4.nextInt(size4 - i)));
        }

        //显示热销商品
        MarketGoodsExample marketHotGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria hotCriteria = marketHotGoodsExample.createCriteria();
        hotCriteria.andIsHotEqualTo(true);
        List<MarketGoods> marketHotGoods = marketGoodsMapper.selectByExample(marketHotGoodsExample);
        int size5 = marketHotGoods.size();
        Random random5 = new Random();
        List<MarketGoods> marketHotGoodsList = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            marketHotGoodsList.add(marketHotGoods.remove(random5.nextInt(size5 - i)));
        }

        //显示专题
        List<MarketTopic> marketTopics = marketTopicMapper.selectByExample(null);

        //分类商品显示
        MarketCategoryExample exampleL1 = new MarketCategoryExample();
        exampleL1.createCriteria().andLevelEqualTo("L1");
        //查询到level是L1的商品
        List<MarketCategory> categoryL1List = marketCategoryMapper.selectByExample(exampleL1);
        //在循环外定义接收goods（第三层）的数组
        List<HomeIndexFloorGoodsListInfo> goodsList = new ArrayList<>();

        //遍历循环，查询每一个L1层里的所有L2
        for (MarketCategory categoryL1 : categoryL1List) {
            MarketCategoryExample exampleL2 = new MarketCategoryExample();
            //每一个L1下的L2类目
            exampleL2.createCriteria().andLevelEqualTo("L2").andPidEqualTo(categoryL1.getId());
            List<MarketCategory> categoryL2List = marketCategoryMapper.selectByExample(exampleL2);
            //遍历每一个L2，查询到L2对应的每一个goods商品
            //某个二级商品分类下的商品
            List<MarketGoods> marketGoodsL2 = new ArrayList<>();
            for (MarketCategory categoryL2 : categoryL2List) {
                MarketGoodsExample exampleL3 = new MarketGoodsExample();
                //判断L2类目下的商品是否还在售卖
                exampleL3.createCriteria().andCategoryIdEqualTo(categoryL2.getId()).andIsOnSaleEqualTo(true);
                List<MarketGoods> goodsListL2 = marketGoodsMapper.selectByExample(exampleL3);
                marketGoodsL2.addAll(goodsListL2);
            }
            //随机显示每个类目下的6个商品
            int size6 = marketGoodsL2.size();
            Random random6 = new Random();
            List<MarketGoods> limitGoods = new ArrayList<>();
            for (int i = 0; i < 6 ; i++) {
                limitGoods.add(marketGoodsL2.remove(random6.nextInt(size6 - i)));
            }
            HomeIndexFloorGoodsListInfo floorGoodsListInfo = new HomeIndexFloorGoodsListInfo(categoryL1.getName(), categoryL1.getId(), limitGoods);
            goodsList.add(floorGoodsListInfo);
        }

        HomeIndexVo homeIndexVo = new HomeIndexVo();
        homeIndexVo.setNewGoodsList(marketGoodsList);
        homeIndexVo.setCouponList(marketCoupons);
        homeIndexVo.setChannel(marketCategoryList);
        homeIndexVo.setBanner(marketAds);
        homeIndexVo.setBrandList(marketBrandList);
        homeIndexVo.setHotGoodsList(marketHotGoodsList);
        homeIndexVo.setTopicList(marketTopics);
        homeIndexVo.setFloorGoodsList(goodsList);

        return homeIndexVo;
    }


}
